<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
         <el-form-item label="教师名称" style="width: 360px">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="选择专业">
        <el-select v-model="form.subjectsId" placeholder="请选择">
          <el-option
            v-for="item in subjects"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
     

      <el-form-item>
        <el-button type="primary" @click="updateTeachers">立即修改</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["id"],
  data() {
    return {
      form: {
        subjectsId: "",
        name: "",
      },
      
      subjects: [],
    };
  },
  //生命周期一进来就获取动态路由参数
  created() {
    //第一种方式￥route
    // console.log("$route", this.$route.params);
    //第二种方式props,需要现在路由配置中中添加props属性,然后再data上添加props:["动态路由名"]
    // console.log("props", this.id);
    this.getTeacherById();
    this.getSubjects();
  },

  methods: {
    //获取一个老师数据
    async getTeacherById() {
      const res = await this.$api.teachers.getById({
        _id: this.$route.params.id,
      }); //传的参数里面一低昂是一个对象
        // console.log(res.code);
      if (res.code) {
      
        this.form = res.data;
      }
    },
    //获取全部专业数据
    async getSubjects() {
      const res = await this.$api.subjects.get();
    //   console.log(res);
      if (res.code) {
        this.subjects = res.data.rows;
        console.log(this.subjects);
      }
    },
    //修改老师
    async updateTeachers() {
      const res = await this.$api.teachers.update(this.form);
      if (res.code) {
        this.$message({
          message: "修改老师成功",
          type: "success",
        });
        this.$router.push("/home/teacherList");
      } else {
        this.$message.error("修改老师失败");
      }
    },
  },
};
</script>

<style>
.el-input {
  width: 280px;
}
.el-select {
  width: 280px;
}
</style>
